/*
 *@(#)MappingSqlQueryResultSet.java 1.0 Feb 19, 2008
 *Copyright Fution Bai
 *
 */
package codebush.service.support.rowMapper;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.support.rowset.SqlRowSet;

import codebush.domain.Article;

/**
 * actually this id a rowMapper for v_articlen.
 * @author Fution Bai
 * @since 1.0
 */
public class RowMapperForArticle {

	public static RowMapperForArticle getInstance() {
		return new RowMapperForArticle();
	}

	/**
	 * o-r map for Article
	 * 
	 * @param srs
	 * @param rowNum how many rows will be set
	 * @param contentLength the length of the content to set. if it's 0, the whole content will be set
	 * @return
	 */
	public List<Article> mapRow(SqlRowSet srs, int rowNum, int contentLength) {
		List<Article> list = new ArrayList<Article>();
		while (srs.next()) {
			Article article = new Article();
			article.setId(srs.getLong("id"));
			if (srs.getString("title") != null && !srs.getString("title").equals("")) {
				article.setTitle(srs.getString("title"));
			}
			if (contentLength > 0 && srs.getString("content").length() > contentLength) {
				article.setContent(srs.getString("content").subSequence(0, contentLength).toString() + "......");
			} else {
				article.setContent(srs.getString("content"));
			}
			article.setPost_time(srs.getDate("post_time"));
			article.setUser_id(srs.getInt("user_id"));
			article.setReward(srs.getInt("reward"));
			article.setType(srs.getInt("type"));
			article.setUser_name(srs.getString(8));
			article.setPicture((byte[]) srs.getObject("picture"));
			article.setTag_name(srs.getString(10));
			list.add(article);
		}
		return list;
	}

	public List<Article> mapRow(SqlRowSet srs) {
		return mapRow(srs, 0, 0);
	}

	/**
	 * mapRow for article use the specific columns
	 * 
	 * @param srs
	 * @param columns
	 * @param rowNum how many rows will be set
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Article> mapRowForSpecificColumns(SqlRowSet srs, String[] columns, int rowNum) {
		List<Article> list = new ArrayList<Article>();

		int i = 0;
		Boolean args = new Boolean(true);
		while (i < columns.length) {
			try {
				this.getClass().getMethod("set" + columns[i].substring(0, 1).toUpperCase() + columns[i].substring(1), new Class[] { Boolean.class }).invoke(this, args);
			} catch (Exception e) {
				e.printStackTrace();
			}
			i++;
		}

		while (srs.next()) {
			Article article = new Article();
			if (isId == true) {
				article.setId(srs.getLong("id"));
			}
			if (isTitle == true) {
				article.setTitle(srs.getString("title"));
			}
			if (isContent == true) {
				article.setContent(srs.getString("content"));
			}
			if (isPost_time == true) {
				article.setPost_time(srs.getDate("post_time"));
			}
			if (isUser_id == true) {
				article.setUser_id(srs.getInt("user_id"));
			}
			if (isReward == true) {
				article.setReward(srs.getInt("reward"));
			}
			if (isUser_name == true) {
				article.setUser_name(srs.getString(9));
			}
			if (isPicture == true) {
				article.setPicture((byte[]) srs.getObject("picture"));
			}
			if (isTag_name == true) {
				article.setTag_name(srs.getString(10));
			}
			list.add(article);
		}
		return list;
	}

	private boolean isId = false;
	private boolean isTitle = false;
	private boolean isContent = false;
	private boolean isPost_time = false;
	private boolean isUser_id = false;
	private boolean isReward = false;
	private boolean isUser_name = false;
	private boolean isPicture = false;
	private boolean isTag_name = false;

	public void setId(Boolean isId) {
		this.isId = isId;
	}

	public void setTitle(Boolean isTitle) {
		this.isTitle = isTitle;
	}

	public void setContent(Boolean isContent) {
		this.isContent = isContent;
	}

	public void setPost_time(Boolean isPost_time) {
		this.isPost_time = isPost_time;
	}

	public void setUser_id(Boolean isUser_id) {
		this.isUser_id = isUser_id;
	}

	public void setReward(boolean isReward) {
		this.isReward = isReward;
	}

	public void setUser_name(boolean isUser_name) {
		this.isUser_name = isUser_name;
	}

	public void setPicture(boolean isPicture) {
		this.isPicture = isPicture;
	}

	public void setTag_name(boolean isTag_name) {
		this.isTag_name = isTag_name;
	}

}
